﻿namespace HRM.CORE.CLASS
{
    using DevExpress.XtraEditors;
    using DevExpress.XtraReports.UI;
    using Common.Class; using APP.HELPER.Utils.Language; using Security.Security; using Dictionary.ERP;
    using HRM.CORE;
    using Extra;
    
    
    using System; using Extra.ERP;
    using System.Data;
    using System.IO;
    using System.Net.Mail;
    using System.Runtime.CompilerServices;
    using System.Threading;
    using System.Windows.Forms; using Common;
    using Extra.ERP;
    using APP.HELPER.Utils.Net;

    public class SendReportMail
    {
        private string m_content = "";
        private string m_fileName = "";
        private string m_fromAddress = "";
        private string m_fromPassword = "";
        private string m_path = (Application.StartupPath + @"\Layout\email.xml");
        private string m_subject = "";
        private string m_toAddress = "";
        private string m_toPerson = "";
        private int m_Type = 1;
        private XtraReport m_xtraReport;

        public event LoginSuccessHander LoginSuccessed;

        public event SendHander Sended;

        public void DeleteEmail()
        {
            try
            {
                DataSet set = new DataSet();
                using (DataTable table = new DataTable())
                {
                    table.Columns.Add("Email", typeof(string));
                    table.Columns.Add("Password", typeof(string));
                    table.Rows.Add(new object[] { "", "" });
                    set.Tables.Add(table);
                }
                set.WriteXml(this.m_path);
            }
            catch
            {
            }
        }

        public bool IsLoginMail()
        {
            this.ReadXml();
            if ((this.m_fromAddress == "") || (this.m_fromPassword == ""))
            {
                return false;
            }
            return true;
        }

        public void LoginMail()
        {
            SYS_USER sys_user = new SYS_USER();
            sys_user.GetUserName(MyLogin.Account);
            xfmLoginMail mail = new xfmLoginMail(sys_user.Email);
            mail.Login += new xfmLoginMail.LoginHandler(this.xfm_Login);
            mail.ShowDialog();
        }

        public void LoginMail(int Type)
        {
            this.m_Type = Type;
            SYS_USER sys_user = new SYS_USER();
            sys_user.GetUserName(MyLogin.Account);
            xfmLoginMail mail = new xfmLoginMail(sys_user.Email);
            mail.Login += new xfmLoginMail.LoginHandler(this.xfm_Login);
            mail.ShowDialog();
        }

        private void RaiseLoginSuccessHander()
        {
            if (this.LoginSuccessed != null)
            {
                this.LoginSuccessed(this);
            }
        }

        private void RaiseSendHander(string Content, bool Attach)
        {
            if (this.Sended != null)
            {
                this.Sended(this, Content, Attach);
            }
        }

        public void ReadXml()
        {
            try
            {
                DataSet set = new DataSet();
                set.ReadXml(this.m_path);
                this.m_fromAddress = set.Tables[0].Rows[0][0].ToString();
                this.m_fromPassword = set.Tables[0].Rows[0][1].ToString();
            }
            catch
            {
            }
        }

        public void Send(string toAddress, string toPerson, string subject, string content, XtraReport rpt, string fileName)
        {
            this.m_toAddress = toAddress;
            this.m_toPerson = toPerson;
            this.m_subject = subject;
            this.m_content = content;
            this.m_xtraReport = rpt;
            this.m_fileName = fileName;
            this.ReadXml();
            Options.SetWaitDialogCaption("Đang gửi mail...");
            if (this.SendMail(true))
            {
                Options.HideDialog();
            }
            else
            {
                Options.HideDialog();
                XtraMessageBox.Show("Xảy ra lổi trong qu\x00e1 tr\x00ecnh gửi mail!\nVui l\x00f2ng kiểm tra lại địa chỉ email v\x00e0 password của bạn!", "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                SYS_USER sys_user = new SYS_USER();
                sys_user.GetUserName(MyLogin.Account);
                xfmLoginMail mail = new xfmLoginMail(sys_user.Email);
                mail.Login += new xfmLoginMail.LoginHandler(this.xfm_Login);
                mail.ShowDialog();
            }
        }

        public void Send(string toAddress, string toPerson, string subject, string content, XtraReport rpt, string fileName, bool attach)
        {
            this.m_toAddress = toAddress;
            this.m_toPerson = toPerson;
            this.m_subject = subject;
            this.m_content = content;
            this.m_xtraReport = rpt;
            this.m_fileName = fileName;
            this.ReadXml();
            Options.SetWaitDialogCaption("Đang gửi mail...");
            if (this.SendMail(attach))
            {
                Options.HideDialog();
            }
            else
            {
                Options.HideDialog();
                XtraMessageBox.Show("Xảy ra lổi trong qu\x00e1 tr\x00ecnh gửi mail!\nVui l\x00f2ng kiểm tra lại địa chỉ email v\x00e0 password của bạn!", "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                SYS_USER sys_user = new SYS_USER();
                sys_user.GetUserName(MyLogin.Account);
                xfmLoginMail mail = new xfmLoginMail(sys_user.Email);
                mail.Login += new xfmLoginMail.LoginHandler(this.xfm_Login);
                mail.ShowDialog();
            }
        }

        private bool SendMail(bool attach)
        {
            MemoryStream stream = new MemoryStream();
            this.m_xtraReport.ExportToPdf(stream);
            stream.Seek(0L, SeekOrigin.Begin);
            bool result;
            System.Net.Mail.Attachment att = new System.Net.Mail.Attachment(stream, this.m_fileName + ".pdf", "application/pdf");
            try
            {
                Email email = new Email(this.m_fromAddress, this.m_toPerson);
                email.AddToAddress(this.m_toAddress, this.m_toPerson);
                email.AssignGmailSSL(this.m_fromAddress, this.m_fromPassword);
                email.Content(this.m_subject, this.m_content);
                if (attach)
                {
                    email.Attachment(att);
                }
                Thread thread = new Thread(new ThreadStart(()  => //delegate
                 {
                     email.Send();
                 }));
                //thread.Start();
                result = true;
            }
            catch (Exception)
            {
                result = false;
                return result;
            }
            return result;
        }

        public void WriteXml(string Email, string Password)
        {
            try
            {
                DataSet set = new DataSet();
                using (DataTable table = new DataTable())
                {
                    table.Columns.Add("Email", typeof(string));
                    table.Columns.Add("Password", typeof(string));
                    table.Rows.Add(new object[] { Email, Password });
                    set.Tables.Add(table);
                }
                set.WriteXml(this.m_path);
            }
            catch
            {
            }
        }

        private void xfm_Login(object sender, string Email, string Password)
        {
            (sender as XtraForm).Close();
            this.m_fromAddress = Email;
            this.m_fromPassword = Password;
            this.WriteXml(this.m_fromAddress, this.m_fromPassword);
            clsAllOption option = new clsAllOption();
            if (!option.ShowSendMail)
            {
                this.RaiseLoginSuccessHander();
            }
            else
            {
                xfmMailContent xfmMailContent = new xfmMailContent();
                xfmMailContent.Init(this.m_Type);
                xfmMailContent.Sended += delegate (object s, string content, bool attach) {
                    xfmMailContent.Close();
                    this.RaiseSendHander(content, attach);
                };
                xfmMailContent.ShowDialog();
            }
        }

        public string FromAddress
        {
            get
            {
                return this.m_fromAddress;
            }
            set
            {
                this.m_fromAddress = value;
            }
        }

        public string FromPassword
        {
            get
            {
                return this.m_fromPassword;
            }
            set
            {
                this.m_fromPassword = value;
            }
        }

        public delegate void LoginSuccessHander(object sender);

        public delegate void SendHander(object sender, string Content, bool Attach);
    }
}

